﻿using System;


using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using ttbit.math;

namespace ttbit.form.unity
{
    interface ITextureNative
    {
        UnityEngine.Texture NativeTexture
        {
            get;
        }
    }

    class Texture2D : ITexture, ITextureNative
    {
        public UnityEngine.Texture2D tex;
        public UnityEngine.Texture NativeTexture
        {
            get
            {
                return tex;
            }
        }
        public Texture2D(UnityEngine.Texture2D tex)
        {
            this.tex = tex;
            if (this.tex.format == UnityEngine.TextureFormat.ARGB32 || this.tex.format == UnityEngine.TextureFormat.RGBA32)
                format = TextureFormat.RGBA32;
            else if (this.tex.format == UnityEngine.TextureFormat.R8) 
                format = TextureFormat.R8;
            else
                throw new Exception("not supported." + this.tex.format);
        }
        public uint ID => (uint)tex.GetInstanceID();

        public TextureFormat format
        {
            get;
            private set;
        }

        public int width => tex.width;

        public int height => tex.height;

        public bool isStatic
        {
            get;
            set;
        }

        public void Dispose()
        {
            UnityEngine.Object.Destroy(tex);
            tex = null;
        }
    }
    class RenderTarget : IRenderTarget,ITextureNative
    {
        public UnityEngine.RenderTexture rt;
        public UnityEngine.Texture NativeTexture
        {
            get
            {
                return rt;
            }
        }
        Graphic graphic;
        public RenderTarget(UnityEngine.RenderTexture renderTexture, Graphic graphic)
        {
            this.graphic = graphic;
            this.rt = renderTexture;
            if (rt != null)
            {
                if (this.rt.format == UnityEngine.RenderTextureFormat.R8)
                    format = TextureFormat.R8;
                else if (this.rt.format == UnityEngine.RenderTextureFormat.ARGB32)
                    format = TextureFormat.RGBA32;
                else
                    throw new Exception("not supported.");
            }
            this.LookAt = Vector2.Zero;
            this.Scale = 1.0f;
        }
        public uint ID => rt == null ? 0 : (uint)rt.GetInstanceID();
        public bool isStatic => true;
        public TextureFormat format
        {
            get;
            private set;
        }
        public int width => rt == null ? UnityEngine.Screen.width : rt.width;

        public int height => rt == null ? UnityEngine.Screen.height : rt.height;
        public ColorF ClearColor
        { get; set; }
        public Vector2 LookAt { get; set; }
        public float Scale { get; set; }

        public void Dispose()
        {
            if (rt != null)
            {
                UnityEngine.Object.Destroy(rt);
                graphic.allrt.Remove(this);
            }
            rt = null;
        }
    }
}